package system.wrap_filter;

import models.SubscribeEvents;

import java.util.ArrayList;

import com.hong.alert.dao.ReferenceDataRepossitory;

import system.main.SystemDataStructure;
import system.queues.AlertQueue;
import system.queues.LogQueue;
import system.queues.WarningQueue;

public class Filter {
	Standard standard;
	ArrayList<Policy> policies;
	SubscribeEvents subscribeEvents;
	AlertQueue alertQueue;
	WarningQueue warningQueue;
	ReferenceDataRepossitory referenceRepository;
	LogQueue logQueue;

	public Filter(Standard standard, ArrayList<Policy> policies,
			SystemDataStructure ds) {
		this.standard = standard;
		this.subscribeEvents = ds.getSubscribeEvents();
		this.policies = policies;
		this.referenceRepository = ds.getReferenceDataRepossitory();
		this.logQueue = ds.getLogQueue();
		alertQueue = ds.getAlertQueue();
		warningQueue = ds.getWarningQueue();
		this.filtering();
	}

	public void filtering() {
		if (policies.get(0).getSource().equals("hardware")) {
			for (Policy policy : policies) {
				if (standard.getState().equals(policy.getState())
						&& policy.getState().equals("dead")) {
					// hardware alert here
					standard.setAttribute(policy.getType());
					alertQueue.offer(standard);
					subscribeEvents.addAlert();
				} else if (standard.getState().equals(policy.getState())) {
					System.out.println();
					if (standard.getTemperature() > policy.getTemperature()) {
						// hardware warning here
						standard.setAttribute(policy.getType());
						warningQueue.offer(standard);
						subscribeEvents.addWarning();
					} else {
						// hardware normal here
						standard.setAttribute("normal");
						subscribeEvents.addNormal();
					}
				}
			}
		} else if (policies.get(0).getSource().equals("software")) {
			boolean flag = true;
			for (Policy policy : policies) {
				if (standard.getMessage().equals(policy.getMessage())) {
					if (policy.getMessage().equals("alert1")
							|| policy.getMessage().equals("alert2")) {
						// software alert here
						standard.setAttribute(policy.getType());
						alertQueue.offer(standard);
						flag = false;
						subscribeEvents.addAlert();
					} else if (policy.getMessage().equals("warning1")
							|| policy.getMessage().equals("warning2")) {
						// software warning here
						standard.setAttribute(policy.getType());
						warningQueue.offer(standard);
						flag = false;
						subscribeEvents.addWarning();
					}
				}
			}
			if (flag) {
				// software normal here
				standard.setAttribute("normal");
				subscribeEvents.addNormal();
			}
		}
		logQueue.offer(standard);
	}
}
